﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using CakeAnimation;

namespace CharacterAnimationEditor
{
    public class PlayGUI
    {
        public bool LockRoot;
        public AnimationSequence Sequence;

        Skeleton m_Skeleton;

        float m_Speed;

        SkeletonView m_View;
        SkeletonView m_ViewLocked;
        Texture2D m_CircleTexture;


        public PlayGUI(Context context)
        {
            m_Skeleton = new Skeleton(context.AnimationContext, "SkeletonRig.txt");
            Sequence = new AnimationSequence(new List<Animation>());
            m_View = new SkeletonView();
            m_View.MySkeleton = m_Skeleton;
            m_View.Offset = new Vector2(100, 400);
            m_View.Scale = 0.3f;

            m_ViewLocked = new SkeletonView();
            m_ViewLocked.MySkeleton = m_Skeleton;
            m_ViewLocked.Offset = new Vector2(600, 400);
            m_ViewLocked.Scale = 0.3f;

            m_CircleTexture = context.Content.Load<Texture2D>("Circle");

            m_Speed = 1.0f;
        }

        public void Update(Context context)
        {
            if (context.MousePos.Y > 480)
            {
                if (context.CurrKeyState.IsKeyDown(Keys.Left))
                {
                    m_Speed -= context.DeltaTime;
                }
                if (context.CurrKeyState.IsKeyDown(Keys.Right))
                {
                    m_Speed += context.DeltaTime;
                }

                m_Speed = Math.Max(0.02f, Math.Min(1.0f, m_Speed));
            }

            Sequence.Update(context.DeltaTime * m_Speed, m_Skeleton);
            if (Sequence.IsDone())
            {
                Sequence.Reset();
            }


        }

        public void Draw(Context context)
        {
            // Draw animated pose at time t
            m_View.Scale = context.Zoom;
            m_View.Draw(context);

            // Draw pose locked at X axis
            m_Skeleton.RootBone.LocalPosition.X = 0.0f;// = Vector2.Zero;
            m_Skeleton.RootBone.UpdateTransform();
            m_ViewLocked.Draw(context);

            // Draw current keyframe
            Animation anim = Sequence.GetCurrentAnimation();
            if (anim != null && anim.Name == context.Animation.Name)
            {
                anim.GetPoseAt(context.CurrentKeyFrame.Time, m_Skeleton);
                m_Skeleton.RootBone.LocalPosition += Sequence.GetCurrentOffset();
                m_Skeleton.RootBone.UpdateTransform();

                m_View.Alpha = 0.35f;
                m_View.Draw(context);
                m_View.Alpha = 1.0f;

                //Vector2 rootpos = skeleton.RootBone.GetCenter() * view.Scale * context.GetWindowScale() + view.Offset * context.GetWindowScale();
                //context.Sb.DrawString(
                //context.Font,
                //Sequence.currTime.ToString(),
                //rootpos + new Vector2(0, 30),
                //Color.Black);


                for (float t = 0; t < anim.GetDuration(); t += 0.1f)
                {
                    anim.GetPoseAt(t, m_Skeleton);
                    m_Skeleton.RootBone.LocalPosition += Sequence.GetCurrentOffset();
                    m_Skeleton.RootBone.UpdateTransform();

                    Bone bone = m_Skeleton.RootBone;
                    Vector2 pos = bone.GetCenter() * m_View.Scale * context.GetWindowScale() + m_View.Offset * context.GetWindowScale();

                    //context.Sb.Draw(
                    //    circle,
                    //    pos,
                    //    Color.White
                    //    );

                    //context.Sb.DrawString(
                    //context.Font,
                    //t.ToString(),
                    //pos + new Vector2(0,30),
                    //Color.Black);

                }
            }

        }
    }
}
